package com.example.campuspass.controller;

import com.example.campuspass.entity.Student;
import com.example.campuspass.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // 标记这个类提供RESTful API
@RequestMapping("/api/student") // 定义API的基础路径
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 处理学生信息提交的POST请求
     * @param student 从前端JSON数据自动映射到的Student对象
     * @return 操作结果
     */
    @PostMapping("/submit")
    public String submitInfo(@RequestBody Student student) {
        boolean isSuccess = studentService.saveStudentInfo(student);
        if (isSuccess) {
            return "信息提交成功！";
        } else {
            return "信息提交失败，请重试或联系管理员。";
        }
    }

    /**
     * 根据班级查询学生的GET请求
     * @param className 班级名称，通过URL参数传递
     * @return 学生列表
     */
    @GetMapping("/query/by-class")
    public List<Student> queryByClass(@RequestParam String className) {
        return studentService.getStudentsByClass(className);
    }
}
